/*
    
    Copyright (C) 2011  Antoine

 	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
   	the Free Software Foundation; either version 3 of the License, or
   	(at your option) any later version.

   	This program is distributed in the hope that it will be useful,
   	but WITHOUT ANY WARRANTY; without even the implied warranty of
   	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   	GNU General Public License for more details.

   	You should have received a copy of the GNU General Public License
   	along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/

package subs;

 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Test extends JFrame implements ActionListener {
  Test(String title) {
    super(title);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();

    JButton jb = new JButton("Open ...");
    jb.addActionListener(this);
    p.add(jb);

    jb = new JButton("Save ...");
    jb.addActionListener(this);
    p.add(jb);

    getContentPane().add(p);

    setSize(200, 65);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e) {
    int retVal;

    JFileChooser fc = new JFileChooser();

    if (e.getActionCommand().equals("Open ...")) {
      fc.setFileFilter(new FileNameExtensionFilter("Subtile", "srt"));
      //fc.addChoosableFileFilter(new TextFilter());
      retVal = fc.showOpenDialog(this);
    } else
      retVal = fc.showSaveDialog(this);

    if (retVal == JFileChooser.APPROVE_OPTION)
      System.out.println(fc.getSelectedFile().getName());
  }

  public static void main(String[] args) {
    new Test("FileChooser Demo");
  }
}

class TextFilter extends FileFilter {

  public boolean accept(File f) {
    if (f.isDirectory())
      return true;
    String s = f.getName();
    int i = s.lastIndexOf('.');

    if (i > 0 && i < s.length() - 1)
      if (s.substring(i + 1).toLowerCase().equals("txt"))
        return true;

    return false;
  }

  public String getDescription() {
    return "Accepts txt files only.";
  }
}
